//-----------------------------------------------------------------------
// <copyright file="TextFieldDefinition.cs" company="Jamie Knight">
// Copyright (c) Jamie Knight. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.ComponentModel.DataAnnotations;
using System.Windows.Controls;
using System.Windows.Data;
using ValidationResult = System.Windows.Controls.ValidationResult;

namespace DevKnightlie.WPF.AutoForm.FieldDefinitions
{
    /// <summary>
    /// Validation rule which checks minimum and maximum length of a string
    /// </summary>
    public class TextFieldValidationRule : ValidationRule
    {

        public int MinimumLength { get; set; }

        public int MaximumLength { get; set; }

        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            bool result = true;
            String s = value as String ?? "";
            if (MinimumLength > 0 && s.Length < MinimumLength) { result = false; }
            if (MaximumLength > 0 && s.Length > MaximumLength) { result = false; }
            return new ValidationResult(result, null);
        }
    }


    /// <summary>
    /// Field Definition class for text fields
    /// </summary>
    public class TextFieldDefinition: BaseFieldDefinition
    {
        #region Private Fields

        private int _maxLength;
        private int _minLength;

        #endregion


        #region Properties

        /// <summary>
        /// MaximumValue text length
        /// </summary>
        public int MaxLength
        {
            get { return _maxLength; }
            set { _maxLength = value; }
        }


        /// <summary>
        /// MinimumValue text length
        /// </summary>
        public int MinLength
        {
            get { return _minLength; }
            set { _minLength = value; }
        }

        #endregion

        #region Methods

        public override void BuildInputControl()
        {
            TextBox textBox = new TextBox()
            {
                TextAlignment = System.Windows.TextAlignment.Left,
                VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
                Name=String.Format("ed{0}", PropertyName)
            };
            if (MaxLength > 0)
            {
                textBox.MaxLength = MaxLength;
            }
            Binding textBinding = new Binding(PropertyName)
            {
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };
            if (PropertyInfo != null)
            {
                // See if there's a MaxLength attribute against the property
                var attrs = (MaxLengthAttribute[])PropertyInfo.GetCustomAttributes(typeof(MaxLengthAttribute), false);
                if (attrs.Length > 0)
                {
                    MaxLengthAttribute attr = attrs[0];
                    textBox.MaxLength = attr.Length;
                }
            } 
            TextFieldValidationRule rule = new TextFieldValidationRule()
            {
                MinimumLength = MinLength,
                MaximumLength = textBox.MaxLength
            };
            textBinding.ValidationRules.Add(rule);
            BindingOperations.SetBinding(textBox, TextBox.TextProperty, textBinding);
            SetupEnabledBinding(textBox);
            ContainerControl = InputControl = textBox;
        }

        #endregion

    }
}
